/**
 * Created by YHQ on 2016-03-29.
 */
angular.module('hyt.shrService',[])
    //获取承运单列表数据
    .factory('ShrCydListFactory',function($resource, $rootScope,ENV,Storage,$ionicLoading,$state){
        var APIUrl =  Storage.get('fwqdz'),qsResult,jsResult,
            cydListData = {},
            pagesize = 10;

        var resource = $resource(APIUrl);

        return {
            //获取客户未签收列表数据
            getCydList: function(dw_id,sid,user_id,yh_id,zd_id) {
                var hasNextPage =true;    //判断是否有下一页
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME: 'sqlKeyBiz.page#yundanwq_query',
                    ACTION_INFO: {
                        'dw_id':dw_id,
                        'pagesize':'10',
                        'current_page':'1',
                        'shbm_id' :zd_id,
                        'yh_id':yh_id
                    }
                }, function(r) {
                    if(r.ACTION_RETURN_CODE == "000000"){
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }
                        cydListData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : 2,
                            'data' : r.ACTION_INFO.RESULT_LIST
                        };
                        $rootScope.$broadcast('CydList.cydUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });
            },
            //获取承运单列表单页数据
            getCyd : function(){
                if(cydListData[pagesize] == undefined){
                    return false;
                }
                return cydListData[pagesize];
            },

            //获取承运单列表更多页数据
            getMoreCyd:function(dw_id,sid,user_id,yh_id,zd_id){

                //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
                if(cydListData[pagesize] == undefined){
                    return false;
                }

                //获取以前的数据
                var hasNextPage = cydListData[pagesize].hasNextPage;
                var current_page = cydListData[pagesize].current_page;
                var moreCydListData = cydListData[pagesize].data;

                //console.log(moreCydListData);

                resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME: 'sqlKeyBiz.page#yundanwq_query',
                    ACTION_INFO: {
                        'dw_id':dw_id,
                        'pagesize':pagesize,
                        'current_page':current_page,
                        'shbm_id' :zd_id,
                        'yh_id':yh_id
                    }
                }, function (r) {
                    current_page++;

                    if(r.ACTION_RETURN_CODE == "000000"){
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }
                        moreCydListData = moreCydListData.concat(r.ACTION_INFO.RESULT_LIST);

                        cydListData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : current_page,
                            'data' : moreCydListData
                        };

                        $rootScope.$broadcast('CydList.cydUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                })
            },
            hasNextPage: function() {
                if (cydListData[pagesize] === undefined) {
                    return false;
                }
                return cydListData[pagesize].hasNextPage;
            },
            qianshou:function(dw_id,sid,user_id,yh_id,cyd_id,zd_id,shrmc,shrdh,sfzh,hwsl,bmmc){
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME:'sqlKeyBiz.transUpdate#yonghuqianshou_set',
                    ACTION_INFO:{
                        'dw_id':dw_id,
                        'cyd_id':cyd_id,
                        'bm_id':zd_id,
                        'qsrmc':shrmc,
                        'qsrdh':shrdh,
                        'sfzh':sfzh,
                        'yh_id':yh_id,
                        'qssl':hwsl,
                        'bmmc':bmmc
                    }
                },function(resp){
                    qsResult=resp.ACTION_RETURN_CODE;
                    console.log(qsResult);
                    $rootScope.$broadcast('QsUpdated');
                })
            },
            getQsResult:function(){
                return qsResult;
            },
            jushou:function(dw_id,sid,user_id,yh_id,cyd_id,msg,zsxm,lrsj,zd_id,bmmc){
                return resource.save({
                    SID : sid,
                    USER_ID : user_id,
                    ACTION_NAME: "sqlKeyBiz.transInsert#yonghujushou_set",
                    ACTION_INFO : {
                        'bm_id':zd_id,
                        'dw_id' : dw_id,
                        'cyd_id':cyd_id,
                        'yh_id': yh_id,
                        'zsxm':zsxm,
                        'hsbz':msg.hs,
                        'lrsj':lrsj,
                        'bz':msg.bz,
                        'shsl':msg.hsun,
                        'dssl':msg.hc,
                        'bmmc':bmmc
                    }
                },function(r){
                    jsResult=r;
                    console.log(jsResult);
                    $rootScope.$broadcast('JsUpdated');
                })
            },
            getJsResult:function(){
                return jsResult;
            }
        };
    })

    .factory('YqListFactory',function($resource, $rootScope,ENV,Storage,$ionicLoading,$state){
        var APIUrl =  Storage.get('fwqdz'),
            cydListData = {},
            pagesize = 10;

        var resource = $resource(APIUrl);

        return {
            //获取客户已签收列表数据
            getCydList: function(dw_id,sid,user_id,yh_id,zd_id) {
                var hasNextPage =true;    //判断是否有下一页
                return resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME: 'sqlKeyBiz.page#yundanyq_query',
                    ACTION_INFO: {
                        'dw_id':dw_id,
                        'pagesize':'10',
                        'current_page':'1',
                        'shbm_id' :zd_id,
                        'yh_id':yh_id
                    }
                }, function(r) {
                    if(r.ACTION_RETURN_CODE == "000000"){
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }
                        cydListData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : 2,
                            'data' : r.ACTION_INFO.RESULT_LIST
                        };
                        $rootScope.$broadcast('YqList.cydUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                });
            },
            //获取承运单列表单页数据
            getCyd : function(){
                if(cydListData[pagesize] == undefined){
                    return false;
                }
                return cydListData[pagesize];
            },

            //获取承运单列表更多页数据
            getMoreCyd:function(dw_id,sid,user_id,yh_id,zd_id){

                //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
                if(cydListData[pagesize] == undefined){
                    return false;
                }

                //获取以前的数据
                var hasNextPage = cydListData[pagesize].hasNextPage;
                var current_page = cydListData[pagesize].current_page;
                var moreCydListData = cydListData[pagesize].data;

                //console.log(moreCydListData);

                resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME: 'sqlKeyBiz.page#yundanyq_query',
                    ACTION_INFO: {
                        'dw_id':dw_id,
                        'pagesize':pagesize,
                        'current_page':current_page,
                        'shbm_id' :zd_id,
                        'yh_id':yh_id
                    }
                }, function (r) {
                    current_page++;

                    if(r.ACTION_RETURN_CODE == "000000"){
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }
                        moreCydListData = moreCydListData.concat(r.ACTION_INFO.RESULT_LIST);

                        cydListData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : current_page,
                            'data' : moreCydListData
                        };

                        $rootScope.$broadcast('YqList.cydUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                })
            },
            hasNextPage: function() {
                if (cydListData[pagesize] === undefined) {
                    return false;
                }
                return cydListData[pagesize].hasNextPage;
            }
        };
    })
    //运单查询结果
    .factory('SearchListFactory', function ($resource, $rootScope,ENV,Storage,$state,$ionicLoading) {
        var APIUrl =  Storage.get('fwqdz'),
            cydListData = {},
            pagesize = 10;
        var resource = $resource(APIUrl);
        return({
            queryCyd: function (dw_id,cydh,shrmc,shrdh,sid,user_id,yh_id,zd_id) {
                var hasNextPage =true;
                return resource.save({
                    ACTION_NAME: 'sqlKeyBiz.page#yundan_search',
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_INFO: {
                        'dw_id':dw_id,
                        'pagesize':pagesize,
                        'current_page':'1',
                        'cydh':cydh,
                        'shrmc':shrmc,
                        'shrdh':shrdh,
                        'shbm_id' :zd_id,
                        'yh_id':yh_id
                    }
                }, function (resp) {
                    if(resp.ACTION_RETURN_CODE=='000000'){
                        if(resp.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }
                        cydListData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : 2,
                            'data' : resp.ACTION_INFO.RESULT_LIST
                        };
                        $rootScope.$broadcast('SearchList.cydUpdated');
                    }else if (resp.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                })
            },
            getCyd : function(){
                if(cydListData[pagesize] == undefined){
                    return false;
                }
                return cydListData[pagesize];
            },
            getMoreCyd:function(dw_id,cydh,shrmc,shrdh,sid,user_id,yh_id,zd_id){

                //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
                if(cydListData[pagesize] == undefined){
                    return false;
                }

                //获取以前的数据
                var hasNextPage = cydListData[pagesize].hasNextPage;
                var current_page = cydListData[pagesize].current_page;
                var moreCydListData = cydListData[pagesize].data;

                //console.log(moreCydListData);

                resource.save({
                    SID:sid,
                    USER_ID:user_id,
                    ACTION_NAME: 'sqlKeyBiz.page#yundan_search',
                    ACTION_INFO: {
                        'dw_id':dw_id,
                        'pagesize':pagesize,
                        'current_page':current_page,
                        'cydh':cydh,
                        'shrmc':shrmc,
                        'shrdh':shrdh,
                        'shbm_id' :zd_id,
                        'yh_id':yh_id
                    }
                }, function (r) {
                    current_page++;

                    if(r.ACTION_RETURN_CODE == "000000"){
                        if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                            hasNextPage = false;
                        }
                        moreCydListData = moreCydListData.concat(r.ACTION_INFO.RESULT_LIST);

                        cydListData[pagesize] = {
                            hasNextPage : hasNextPage,
                            'current_page' : current_page,
                            'data' : moreCydListData
                        };
                        $rootScope.$broadcast('SearchList.cydUpdated');
                    }else if (r.ACTION_RETURN_CODE == "000005"){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "您的账号已在其他设备登录",
                            duration: 1500
                        });
                        $state.go("login");
                    }
                })
            },
            hasNextPage: function() {
                if (cydListData[pagesize] === undefined) {
                    return false;
                }
                return cydListData[pagesize].hasNextPage;
            }
        })
    });
